#!/bin/sh

#=========================================
echo ===== SetENV start
#=========================================
echo "Current Path: $PWD"
now_date=$(date +%Y%m%d-%H%M%S)
if ! [ -d $PWD/bck ]; then
	mkdir $PWD/bck
fi

echo "OK"

#=========================================
echo ===== Chmod file
#=========================================
chmod 755 ./*
chmod 755 ./*/*
chmod 755 ./*/*/*
chmod 755 ./*/*/*/*
chmod 755 ./*/*/*/*/*

echo "OK"

#=========================================
echo ===== Clear lib env
#=========================================
cp /etc/ld.so.conf $PWD/bck/ld.so.conf_$now_date

sed '/#zxpisweb-so-path/,/#end-zxpisweb-so-path/'d /etc/ld.so.conf > $PWD/bck/ld.so.conf_tmp
cat $PWD/bck/ld.so.conf_tmp > /etc/ld.so.conf
rm $PWD/bck/ld.so.conf_tmp

#echo "#zxpisweb-so-path" > $PWD/bck/zxpisweb_so_path_tmp
#echo $PWD/lib >> $PWD/bck/zxpisweb_so_path_tmp
#echo "#end-zxpisweb-so-path" >> $PWD/bck/zxpisweb_so_path_tmp
#cat $PWD/bck/zxpisweb_so_path_tmp >> /etc/ld.so.conf

ldconfig

echo "OK"

#=========================================
echo ===== Set java and tomcat env
#=========================================
catalina_sh_path=$PWD/tomcat6/bin/catalina.sh
catalina_bak_sh_path=$PWD/tomcat6/bin/catalina_bak.sh

cp -f $catalina_bak_sh_path $catalina_sh_path

echo {s:__LD_LIBRARY_PATH__:$PWD/lib:g} > $PWD/bck/zxpisweb_sed_0_tmp
sed -i -f $PWD/bck/zxpisweb_sed_0_tmp $catalina_sh_path

echo {s:__CATALINA_HOME__:$PWD/tomcat6:g} > $PWD/bck/zxpisweb_sed_1_tmp
sed -i -f $PWD/bck/zxpisweb_sed_1_tmp $catalina_sh_path

echo {s:__JAVA_HOME__:$PWD/java6:g} > $PWD/bck/zxpisweb_sed_2_tmp
sed -i -f $PWD/bck/zxpisweb_sed_2_tmp $catalina_sh_path

echo {s:__JRE_HOME__:$PWD/java6/jre:g} > $PWD/bck/zxpisweb_sed_3_tmp
sed -i -f $PWD/bck/zxpisweb_sed_3_tmp $catalina_sh_path

echo "OK"
